const Koa=require('koa')
const bodaparser=require('koa-bodyparser')
const Router=require('koa-router')
const {exec}=require('./db')

const app=new Koa()
const router=new Router()

app.use(bodaparser())
app.use(router.routes())


router.post('/regis',async ctx=>{
    let {userName,passWord}=ctx.request.body
    console.log(userName,passWord);
    let data=await exec(`select * from user where userName='${userName}'`)
    if(data.length>0){
        ctx.body={
            msg:'账号已存在'
        }
    }else{
        await exec(`insert into user (userName,passWord) values ('${userName}','${passWord}')`)
        ctx.body={
            msg:'注册成功'
        }
    }
    
})


router.post('/login',async ctx=>{
    let {userName,passWord}=ctx.request.body
    console.log(userName,passWord);
    let data=await exec(`select * from user where userName='${userName}' and passWord=${passWord}`)
    if(data[0].userName==userName && data[0].passWord==passWord){
        ctx.body={
            msg:'登录成功'
        }
    }else{
        ctx.body={
            msg:'账号或密码错误'
        }
    }
})


app.listen(3000,()=>{
    console.log('http://localhost:3000');
})